package cn.changeforyou.web.dao.mybatis.ext.extBuilder;

import cn.changeforyou.web.dao.mybatis.ext.extBuilder.writer.SentenceEnum;
import cn.changeforyou.web.dao.mybatis.ext.extBuilder.writer.SqlBuilder;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.EntityMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.FieldMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.MappedStatementParameterType;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.MappedStatementResultType;
import cn.changeforyou.web.utils.tree.Tree;
import lombok.Data;
import org.apache.ibatis.mapping.SqlCommandType;

import java.util.List;

@Data
public class SqlContext {

    private SqlBuilder sql;
    /**
     * 包含括号
     */
    private boolean hasBracket;
    /**
     * 包含or
     */
    private boolean hasOr;
    /**
     * 需要if test标签
     */
    private boolean needIfTest;
    /**
     * 明确要select的字段
     */
    private List<FieldMetaInfo> selectMetaInfo;

    /**
     * 明确要插入的元字段
     */
    private EntityMetaInfo insertMetaInfo;

    /**
     * 主表信息
     */
    private TableInfoForm mainTableForm;
    /**
     * 关联表信息
     */
    private List<TableInfoForm> relationTableMaps;

    /**
     * 明确要修改的update字段
     */
    private List<FieldMetaInfo> updateMetaInfo;

    /**
     * 修改语句是否要忽视null
     */
    private boolean updateIgnoreNull;

    /**
     * 主键信息
     */
    private FieldMetaInfo primaryKeyInfo;
    /**
     * 返回类型
     */
    private MappedStatementResultType mappedStatementResultType;

    /**
     * sql类型
     */
    private SqlCommandType sqlCommandType;

    /**
     * 参数类型
     */
    private MappedStatementParameterType mappedStatementParameterType;

    /**
     * 是否是脚本模式
     */
    private boolean scriptModel;

    /**
     * "0" 为顶级id的where字句树
     */
    private Tree<String> whereTrees;

    /**
     * 映射器的返回值
     */
    private Class returnType;

    private boolean whereNeedIfTest;

    private String whereJavaParamPrefix;
    private String setJavaParamPrefix;

    private SentenceEnum sentenceEnum;

    public SqlContext() {
        this.sql = new SqlBuilder();
    }

    public boolean mappedStatementParameterTypeIn(MappedStatementParameterType ... types) {
        return MappedStatementParameterType.in(this.mappedStatementParameterType, types);
    }

    public String getCurrentJavaParamPrefix(){
        String javaParamPrefix = null;
        if (sentenceEnum == SentenceEnum.set) {
            javaParamPrefix = setJavaParamPrefix;
        } else if (sentenceEnum == SentenceEnum.where) {
            javaParamPrefix = whereJavaParamPrefix;
        }
        return javaParamPrefix;
    }

}